package com.xywy.fwc;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.FragmentMainActivity;
import com.xywy.common.constants.ConstantValue;
import com.xywy.common.util.ImageLoaderUtil;
import com.xywy.common.util.MD5Util;
import com.xywy.fwc.bean.FwcActivityBean;
import com.xywy.fwc.bean.FwcHomeData;
import com.xywy.fwc.net.DataCallBack;
import com.xywy.fwc.net.Request;
import com.xywy.fwc.paser.FwcActivityParser;
import com.xywy.me.bean.UserKeeper;

/**
 * 服务窗fragment
 * 
 * @author yangyu
 */
public class FwcActivity extends BaseActivity implements OnClickListener{

	
	private LinearLayout lin_reduce;//减肥
	private RelativeLayout lin_healthtest;//测试
	private RelativeLayout rl_ask_doc;
	private RelativeLayout rl_tel;
	private RelativeLayout rl_home_doc;
	private RelativeLayout rl_grugs;

	private ImageView ivMessage;

	private ImageView ivMessagePoint;
	private FwcHomeData data;
	ViewPager pager;
	private List<FwcActivityBean> list_active_bean;
//	ImageView iv_point1;
//	ImageView iv_point2;

	private SharedPreferences sp;

	private String userid;
	private LinearLayout linear_point;
	

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	// TODO Auto-generated method stub
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.fragment_wys);
    	init();
    	this.initView();
    	sp = getSharedPreferences("config", Context.MODE_PRIVATE);
//		userid = sp.getString("userid", "");
    	userid = UserKeeper.getCurrentUser(this).getUserid();
		this.loadData();
		this.setListener();
    }
	
	
	private void init(){
	}
	List<View> views;

	
	List<TextView> list_tv;
	
	private void initActivityPager(){
		views= new ArrayList<View>();
		linear_point.setVisibility(View.VISIBLE);
		list_tv = new ArrayList<TextView>();
		System.out.println("--------------"+list_active_bean.size());
		for(int i=0;i<list_active_bean.size();i++){
			View v = View.inflate(FwcActivity.this, R.layout.fwc_pager_item, null);
			ImageView iv = (ImageView) v.findViewById(R.id.iv);
			iv.setTag(list_active_bean.get(i));
			ImageLoaderUtil.displayImage(list_active_bean.get(i).getImg(), iv);
			iv.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					FwcActivityBean bean = (FwcActivityBean) v.getTag();
					Intent intent = new Intent(FwcActivity.this,FwcNewWebAct.class);
					intent.putExtra("title", bean.getTitle());
					intent.putExtra("url", bean.getUrl());
				    startActivity(intent);
				}
			});
			views.add(v);
			
			TextView tv = new TextView(FwcActivity.this);
			tv.setText("●");
			tv.setTextSize(20);
			LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
			tv.setLayoutParams(params);
			list_tv.add(tv);
			if(list_active_bean.size()>1)
			linear_point.addView(tv);
			
		}
		linear_point.invalidate();
		pager.setAdapter(new PagerAdapter() {
			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public int getCount() {
			return views.size();
		}

		@Override
		public Object instantiateItem(ViewGroup view, int position) {
			((ViewPager) view).addView(views.get(position));
			return views.get(position);
		}

		public void destroyItem(View view, int position, Object object) {
			((ViewPager) view).removeView(views.get(position));
		}
	});
		
		pager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int position) {
				changeTv(position);
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	private void setListener(){
		lin_reduce.setOnClickListener(this);
		lin_healthtest.setOnClickListener(this);
		rl_ask_doc.setOnClickListener(this);
		rl_grugs.setOnClickListener(this);
		rl_home_doc.setOnClickListener(this);
		rl_tel.setOnClickListener(this);
	}
	
	private void changeTv(int index){
		for(int i=0;i<list_tv.size();i++){
			TextView tv = list_tv.get(i);
			tv.setTextColor(0xffeaeaea);
		}
		TextView tv = list_tv.get(index);
		tv.setTextColor(0xff89dad9);
		
	}
	

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.lin_reduce:
			Intent fm = new Intent(FwcActivity.this, FwcMessageInfo.class);
			data.setName("健康减肥");
			data.setId(6+"");
			fm.putExtra("data", data);
			startActivity(fm);
			break;
		case R.id.lin_healthtest:
//			data.setName("血压管家");
//			data.setId(7+"");
//			Intent fm1 = new Intent(FwcActivity.this, FwcMessageInfo.class);
//			fm1.putExtra("data", data);
//			startActivity(fm1);
			break;
		case R.id.rl_home_doc:
			try {
				Intent intent = new Intent(FwcActivity.this,FwcNewWebAct.class);
				intent.putExtra("url", FwcConstant.DOCTOR_URL+"uid="+userid+"&sign="+MD5Util.md5(userid+FwcConstant.H5KEY));
				intent.putExtra("title", "家庭医生");
				startActivity(intent);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case R.id.rl_ask_doc:
			try {
				String url = FwcConstant.H5_PIC_URl+"uid="+userid+"&sign="
						+MD5Util.md5(userid+FwcConstant.H5KEY)+
						"&os=android";
				Intent intent = new Intent(FwcActivity.this,FwcNewWebAct.class);
				intent.putExtra("url", url);
				intent.putExtra("title", "问医生");
				startActivity(intent);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case R.id.rl_drugs:
			String url = "http://cs.3g.yao.xywy.com/";
			Intent intent = new Intent(FwcActivity.this,FwcNewWebAct.class);
			intent.putExtra("url", url);
			intent.putExtra("title", "药品");
			startActivity(intent);
			break;
			//&uid=&username=&sign=
		case R.id.rl_tele:
			try {
				Intent telIntent = new Intent(FwcActivity.this,FwcNewWebAct.class);
				telIntent.putExtra("title", "预约加号");
				String username = UserKeeper.getCurrentUser(FwcActivity.this).getAccountstr();
				telIntent.putExtra("url", FwcConstant.MY_ADDOUT_URL+"&uid="+userid+"&username="+username+"&sign="+
				MD5Util.md5(userid+FwcConstant.ADDKEY));
				startActivity(telIntent);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		default:
			break;
		}
	}
	/**
	 * uid=12234&sign=5d58f7a4faf912b52728085d093252f8
	 */
	
	/**
	 * 友盟统计收藏 帮助
	 */
	public void Stattistics(){
		Map<String, String> params = new HashMap<String, String>();
		params.put("plate","消息盒子"); //代表来源
			MobclickAgent.onEvent(FwcActivity.this, "Fwc_001", params);
	}

	@Override
	public void preInit() {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void initView() {
		// TODO Auto-generated method stub

		lin_reduce = (LinearLayout) findViewById(R.id.lin_reduce);
		lin_healthtest = (RelativeLayout) findViewById(R.id.lin_healthtest);
		rl_ask_doc = (RelativeLayout) findViewById(R.id.rl_ask_doc);
		rl_grugs = (RelativeLayout) findViewById(R.id.rl_drugs);
	    rl_home_doc = (RelativeLayout) findViewById(R.id.rl_home_doc);
	    rl_tel = (RelativeLayout) findViewById(R.id.rl_tele);
//	    iv_everyday = (ImageView) findViewById(R.id.iv_everyday);
	
	    ivMessage = (ImageView) findViewById(R.id.ivMessage);
		ivMessagePoint = (ImageView) findViewById(R.id.ivMessagePoint);
		ivMessage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Stattistics();
				ivMessagePoint.setVisibility(View.GONE);
				Intent intent = new Intent(FwcActivity.this,FwcMessageActivity.class);
				startActivity(intent);
				FwcConstant.setMessageCount(FwcConstant.FWC_MESSAGE, FwcActivity.this, 0);
			}
		});
		pager = (ViewPager) findViewById(R.id.pager);
		linear_point = (LinearLayout) findViewById(R.id.linear_point);
		linear_point.setVisibility(View.INVISIBLE);
	
	}


	@Override
	public void loadData() {

		data = new FwcHomeData();
		data.setName("服务窗");
		int message_count = FwcConstant.getMessageCount(FwcConstant.FWC_MESSAGE, FwcActivity.this);
		if(message_count >0){
			ivMessagePoint.setVisibility(View.VISIBLE);
		}else {
			ivMessagePoint.setVisibility(View.INVISIBLE);
		}
		Request.requestData(new FwcActivityParser(), new DataCallBack<List<FwcActivityBean>>() {

			@Override
			public void dataCall(List<FwcActivityBean> list) {
				list_active_bean = list;
				if(list != null && list.size()>0){
					FwcActivity.this.runOnUiThread(new Runnable() {
						
						@Override
						public void run() {
							initActivityPager();
						}
					});
				}else {
					
				}
			}
		}, FwcConstant.FWC_ACTIVITY_URL);
    
	}
	
}
